package com.bsh.controller.sysconfig;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.bsh.mybatis.model.SysConfig;
import com.bsh.service.SysConfigService;
import com.bsh.utils.PageTools;
import com.bsh.utils.Pager;
import com.mysql.jdbc.StringUtils;

/**
 * @author gsh
 * @since 0.0.1
 * 
 */
@Controller
public class SysConfigController {
	private static Logger log = LoggerFactory.getLogger(SysConfigController.class);
	@Autowired
	HttpServletRequest request;
	@Autowired
	HttpServletResponse response;
	@Autowired
	HttpSession session;
	@Autowired
	SysConfigService cfgService;

	/**
	 * app配置列表
	 */
	@RequestMapping(value = "/sysconfig/app/list")
	public ModelAndView adminList() {
		ModelAndView mv = new ModelAndView();
		mv.setViewName("/pages/sysconfig/appConfigList");

		String pageStr = request.getParameter("pageIndex");
		Integer pageIndex = Integer.parseInt(StringUtils.isNullOrEmpty(pageStr) ? "1" : pageStr);

		String sizeStr = request.getParameter("pageSize");
		Integer pageSize = Integer.parseInt(StringUtils.isNullOrEmpty(sizeStr) ? "10" : sizeStr);

		Pager pager = new Pager(pageIndex, pageSize);
		List<Map<String, Object>> list = cfgService.getAppPage(pager);
		mv.addObject("list", list);
		mv.addObject("pager", pager);
		mv.addObject("nav_title", "app配置管理");
		return mv;
	}
	/**
	 * app配置列表
	 */
	@RequestMapping(value = "/sysconfig/sys/list")
	public ModelAndView sysList() {
		ModelAndView mv = new ModelAndView();
		mv.setViewName("/pages/sysconfig/appConfigList");

		String pageStr = request.getParameter("pageIndex");
		Integer pageIndex = Integer.parseInt(StringUtils.isNullOrEmpty(pageStr) ? "1" : pageStr);

		String sizeStr = request.getParameter("pageSize");
		Integer pageSize = Integer.parseInt(StringUtils.isNullOrEmpty(sizeStr) ? "10" : sizeStr);

		Pager pager = new Pager(pageIndex, pageSize);
		List<Map<String, Object>> list = cfgService.getSysPage(pager);
		mv.addObject("list", list);
		mv.addObject("pager", pager);
		mv.addObject("nav_title", "系统配置管理");
		return mv;
	}
	/**
	 * 修改配置--初使化
	 */
	@RequestMapping(value = "/sysconfig/edit/{id}")
	public ModelAndView edit(@PathVariable(value = "id") Integer adminid) {
		ModelAndView mv = new ModelAndView();
		mv.setViewName("/pages/sysconfig/edit");
		SysConfig admin = cfgService.getConfig(adminid);
		mv.addObject("cfg", admin);
		return mv;
	}

	/**
	 * 修改配置--保存
	 */
	@RequestMapping(value = "/sysconfig/edit/save")
	public ModelAndView editSave(@ModelAttribute SysConfig admin) {
		Integer res = cfgService.editConfig(admin.getId(), admin.getSvalue());
		ModelAndView mv = new ModelAndView();
		mv.setViewName("/pages/sysconfig/edit");
		mv.addObject("cfg", admin);

		String msg = PageTools.alertCloseFraRefresh("修改配置", res>0?"保存成功！":"保存失败", 1000);
		mv.addObject("actionMsg", msg);
		return mv;
	}

}
